<?php

/**
 * @category   Mediad2
 * @package    Mediad2_Service
 */
class Mediad2_Service_Admin_Files extends Mediad2_Service_Abstract
{
	/**
	 * 初始化
	 */
	public function init()
	{
		$this->setMapper('Mediad2_Mapper_Files');
		$this->setModel('Mediad2_Model_Files');
	}
	
	/**
	 * 上传文件
	 * @param int $sourcetype
	 * @param int $sourceid
	 * @param string $upload_name
	 * @param boolean $returnArray
	 * @return Mediad2_Model_Result
	 */
	public function upload($sourcetype, $sourceid, $upload_name='Filedata', $returnArray = false)
	{
		try {
			//上传文件接收和处理
			$fileHandle = new Mediad2_Utility_FileHandle();
			$result = $fileHandle->upload(FILE_LOCATION, $upload_name);
	
			//处理文件出现失败，则终止处理
			if ($result->getCode() == $result::CODE_FAILURE) {
				return $result;
			}
	
			//处理上传文件正常，则继续
			//赋值到data数组中
			$data = $result->getData();
	
			//设置文件存放位置
			$position = Mediad2_Model_Files::POSITION_LOCAL;	//本地
	
			//保存到数据表 ps_files
			//构造model-files
			$model = new Mediad2_Model_Files();
			$model->setFilename($data['filename']);
			$model->setFiletype($data['filetype']);
			$model->setFileimg($data['fileimg']);
			$model->setFilesize($data['filesize']);
			$model->setFilepath($data['filepath']);
			$model->setFiledesc('');
			$model->setSourcetype($sourcetype);
			$model->setSourceid($sourceid);
			$model->setAddtime($data['addtime']);
			$model->setStatus($model::STATUS_NORMAL);
			$model->setPosition($position);					//位置信息
			$model->setImgdate($data['imgdate']);
			$model->setImginfo($data['imginfo']);
	
			//插入到数据表
			$serviceFiles = new Mediad2_Service_Admin_Files();
			$result = $serviceFiles->insert($model);
			//保存数据，则终止处理
			if ($result->getCode() == $result::CODE_FAILURE) {
				return $result;
			}
			
			//返回结果
			$fid = $result->getData();
			$model->setFid($fid);
			if ($returnArray) {
				return $this->returnResult(Mediad2_Model_Result::CODE_SUCCESS,null,$model->toArray());
			} else {
				return $this->returnResult(Mediad2_Model_Result::CODE_SUCCESS,null,$model);
			}
			
	
		} catch (Zend_Exception $e) {
			$this->handleException($e);
			return $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Error saving file, Please try again.');
		}
	}
	
	/**
	 * 下载文件
	 * @param int $fid
	 * @return Mediad2_Model_Result
	 */
	public function download($fid)
	{
		try {
			$file_path = '';
			$file_name = '';
	
			$serviceFiles = new Mediad2_Service_Admin_Files();
			$where = sprintf("fid = %s and status = %s", $fid, Mediad2_Model_Files::STATUS_NORMAL);
			$result = $serviceFiles->findBy($where);
			if ($result->getCode()==$result::CODE_FAILURE) {
				$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Can not get information for the specified file, please try again');
			} else {
				$model = $result->getData();
				$file_name 	= $model->getFilename();
				$fpath 		= $model->getFilepath();
				$position 	= $model->getPosition();
				if (empty($position)) {
					$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'can not get position for the specified file，please try again');
				} else {
					$file_path 	= FILE_LOCATION.$fpath;
					if (! file_exists ( $file_path )) { //检查文件是否存在
						$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'can not get the spfecified file, please try again');
					} else {
						$isdelOriFile = false; //保存在本地，所以不删除文件
						$encoded_filename = urlencode($file_name);
						$encoded_filename = str_replace("+", "%20", $encoded_filename);
						$download = new Mediad2_Utility_Bfdownload($file_path,$encoded_filename);
						$download->download_file($isdelOriFile);
						exit ();
					}
				}
			}
			return $this->result;
	
		} catch (Zend_Exception $e) {
			$this->handleException($e); //出错处理
			return $this->returnResult(Mediad2_Model_Result::CODE_FAILURE); //返回结果--失败
		}
	
	}
	
	/**
	 * unset files
	 * @param array $files
	 * @return array
	 */
	public function unsetFiles(array $files)
	{
		foreach ($files AS $ffk => $ffv) {
			switch ($ffk) {
				case 'filename':
				case 'filetype':
				case 'fileimg':
				case 'filesize':
				case 'filepath':
				case 'filedesc':
					break;
				default:
					unset($files[$ffk]);
				break;
			}
		}
		return $files;
	}
	
}